<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email extends My_Controller{
	
	public static  $m_Type = array(
			DOUBAN_TYPE,
	);
	
	public static $sleepTime = 20;
	
	public static $m_baseID = 1000000;

	public function __construct()
	{
		parent::__construct();
		
		if (empty($this->m_arrUser))
		{
			redirect('account/login?reUrl=index.php/douban/email');
			exit;
		}
	
		$this->load->library('my_api',self::$m_Type);
	
		$this->load->model('msg_m');
	
	}
		
	public function index()
	{
		$this->m_arrSub = array(
				'common/header'  =>  'index',
				'common/footer'  => 'index',
		);
		$this->getSub();
		
		$this->data['ajaxUrl'] = array(
				'following' => "index.php/douban/friend/following",
				'followers' => "index.php/douban/friend/followers",
				'each' => "index.php/douban/friend/each",	
				'add' => "index.php/douban/email/add",
				'send' => "index.php/douban/email/send",
				);
		
		
		
		$this->load->view('douban/email/email',$this->data);
		
	}
	
	public function add()
	{
		$idList = $this->input->post('idList');
		
		$title = $this->input->post('title');
		$content = $this->input->post('content');
		
		
		if ($title == null || $content == null)
		{
			return false;
		}
		
		if (empty($idList))
		{
			return false;
		}
		
		$strId = substr($idList, 0,strlen($idList)-1);
		
		$idList = explode(',', $strId);
		
		// 计算排队时间...
		
		$startTime = null;
		$filter = array(
				'noStatus' => 2,
// 				'order' => 'DESC',
// 				'sort' => 'startTime',
				);
		$msgList = $this->msg_m->getMsg($filter);
		
		if (empty($msgList))
		{
			$startTime = date("Y-m-d H:i:s");
		}
		else{
			$msgIitemOk = $this->msg_m->getMsgList(array('msgID' => $msgList[0]['id'],'status' => 0));
			
			$startTime = date("Y-m-d H:i:s",time() + ($msgList[0]['total'] - count($msgIitemOk)*self::$sleepTime));
		}
		
		$toAdd = array(
				'accountID' => $this->m_arrUser['id'],
				'title' => $title,
				'content' => $content,
				'total' => count($idList),
				'startTime' => $startTime,
				'status' => 0,
				);
		$msgID = $this->msg_m->addMsg($toAdd);
		foreach($idList as $id)
		{
			$toAdd = array(
					'msgID' => $msgID,
					'targetID' => intval($id),
					'status' => 0,
					);
			$this->msg_m->addMsgList($toAdd);
		}
		
		$arrRet = array('status' => 1,'msgID' => $msgID);
		
		echo json_encode($arrRet);
		
	}
	
	/** 发送豆邮 ..
	 * @return boolean
	 */
	public function send()
	{

	   $msgID = $this->input->post('msgID');
	   
	   if ($msgID == null)
	   {
	   	 echo '没有ID';
	   	 return false;
	   }
	   
	   $msgInfo = $this->msg_m->getMsg(array('id' => $msgID));
	   
	   if (empty($msgInfo) || !isset($msgInfo[0]))
	   {
	   	 echo '信息没有';
	   	 return  false;
	   }
	   
	   $msgInfo = $msgInfo[0];
	   
	   // 排队时间..
	   $sleepTime = time() - strtotime($msgInfo['startTime']);
	   
	   echo $sleepTime;
	   sleep($sleepTime);
	   
	   echo '要开始了吧';
	   // 获取消息列表
	   $msgList = $this->msg_m->getMsgList(array('msgID' => $msgID));
	   
	   print_r($msgList);
	   $this->msg_m->setMsg($msgID,array('id' => $msgID,'status' => 1));
	   foreach ($msgList as $msg)
	   {
	   		sleep(DMAILTIME);
		   	$data = array(
		   			'title'			 =>  $msgInfo['title'],
		   			'content' 		 =>  $msgInfo['content'],
		   			'receiver_id' =>  $msg['targetID'],
		   	);
		   	
		   	$ret  = $this->my_api->douban->api('Doumail.mail.POST')->makeRequest($data);
		   	
		   	print_r($ret);
		   	// 成功咯..
		    if (empty($ret))
		    {
		    	echo "成功咯..".$msg['id']."<br>";
		    	$this->msg_m->setMsgList($msg['id'],array('id' => $msg['id'],'status' => 1));
		    }
		    else 
		    {
		    	// TODO: 后期添加验证码破解机制 ...
		    	$this->msg_m->setMsgList($msg['id'],array('id' => $msg['id'],'status' => 2));
		    }
	   }
	   
	   $this->msg_m->setMsg($msgID,array('id' => $msgID,'status' => 2));

	}
	
}